package com.fita.admin.web.controller.finance;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.report.RechargeRecordSearch;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.agent.AgentCompanyService;
import com.fita.admin.service.agent.AgentService;
import com.fita.admin.service.report.RechargeRecordService;
import com.fita.admin.service.wallet.UserWalletService;
import com.fita.admin.web.controller.view.Excel2TemplateView;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.agent.Agent;
import com.fita.entity.agent.AgentCompany;
import com.fita.entity.wallet.RechargeRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 充值记录
 * @author: 11200
 * @date: 2019/7/26
 */
@Controller
@RequestMapping(path = "/recharge-records")
@MenuSetting("menu-report")
public class RechargeRecordController {

    private static final Logger logger = LoggerFactory.getLogger(RechargeRecordController.class);

    @Autowired
    private RechargeRecordService rechargeRecordService;

    @Autowired
    private UserWalletService userWalletService;

    @Autowired
    private AgentCompanyService agentCompanyService;

    @Autowired
    private AgentService agentService;

    /**
     *
     * @param search
     * @param page
     * @param request
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-recharge-record-list")
    public ModelAndView list(RechargeRecordSearch search,
                             Page page,
                             HttpServletRequest request){

        ModelAndView modelAndView = new ModelAndView("report/recharge-record/recharge-record-list");

        int count = rechargeRecordService.count(search);

        if (count > 0){

            List<RechargeRecord> rechargeRecords = rechargeRecordService.search(RechargeRecord.class, search, page.with(count));

            modelAndView.addObject("rechargeRecords", rechargeRecords);

            modelAndView.addObject("page", page);
        }
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

        return modelAndView;
    }

    /**
     * 导出页面
     * @return
     */
    @GetMapping(path = "/export")
    public ModelAndView export(){

        ModelAndView modelAndView = new ModelAndView("report/recharge-record/recharge-record-export");

        List<Agent> agents = agentService.listAll(Agent.class);

        modelAndView.addObject("agents", agents);

        return modelAndView;
    }

    /**
     * 导出
     * @return
     */
    @PostMapping(path = "/export")
    public ModelAndView onExport(RechargeRecordSearch search,
                                 Page page){

        ModelAndView modelAndView = new ModelAndView(new Excel2TemplateView("report/recharge-record-report.xlsx"));

        int count = rechargeRecordService.count(search);

        if (count > 0){

            List<RechargeRecord> rechargeRecords = rechargeRecordService.search(RechargeRecord.class, search, page.with(count));

            modelAndView.addObject("rechargeRecords", rechargeRecords);
        }
        return modelAndView;
    }

    /**
     * recharge offline page
     * @return
     */
    @GetMapping(path = "/offline-recharge")
    public ModelAndView offlineRecharge(){

        ModelAndView modelAndView = new ModelAndView("report/recharge-record/recharge-offline-recharge");

        List<AgentCompany> agentCompanies = agentCompanyService.listAll(AgentCompany.class);

        modelAndView.addObject("agentCompanies", agentCompanies);

        return modelAndView;
    }

    /**
     * recharge offline
     * @return
     */
    @PostMapping(path = "/offline-recharge", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onOfflineRecharge(RechargeRecord rechargeRecord,
                                        @RequestParam Long targetAgentCompanyId,
                                        @Secure Login login){

        HttpResult httpResult = new HttpResult();

        try {
            rechargeRecordService.offlineRecharge(rechargeRecord, targetAgentCompanyId, login.getId());

            return httpResult.success().message("线下支付成功");

        } catch (BusinessException e){

            logger.info("#onOfflineRecharge() recharge offline fail [{}]", e);

            return httpResult.fail("500").message(e.getMessage());

        } catch (Exception e) {

            logger.error("#onOfflineRecharge() error [{}]", e);

            return httpResult.fail("500").message("操作失败");
        }
    }

}
